package cn.lovecyy.mockjson.core.type;

import cn.hutool.core.util.StrUtil;
import cn.lovecyy.mockjson.core.domain.TypeConfig;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * DateTypeMocker
 *
 * @author Yakir
 */
public class DateTypeMocker implements TypeMocker {

	@Override
	public String getType() {
		return "date";
	}

	@Override
	public Object mock(TypeConfig config) {
		String param = config.getParam();
		String format = config.getFormat();
		String mainParam = config.getMainParam();
		String retVal;
		LocalDate curDate;
		if (StrUtil.isEmpty(mainParam)) {
			curDate = LocalDate.now();
		}
		else {
			curDate = LocalDate.parse(mainParam, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
		}
		if (StrUtil.isEmpty(format)) {
			retVal = curDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
		}
		else {
			retVal = curDate.format(DateTimeFormatter.ofPattern(format));
		}
		return retVal;
	}

}
